/*
 * Created on 2005-1-24
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.nauu.atombuilder.modle;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.PositionConstants;
import org.eclipse.ui.views.properties.IPropertyDescriptor;

/**
 * @author nauu
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
@SuppressWarnings("unchecked")
public class Diagram extends Element {
	
	private static final long serialVersionUID = 1L;

	public static String PROP_NODE = "NODE";

	protected Ruler leftRuler, topRuler;
	protected Integer connectionRouter = null;
	private boolean rulersVisibility = true;
	private boolean snapToGeometry = true;
	private boolean gridEnabled = true;	
    protected List nodes = new ArrayList();

    public Diagram(){
    	leftRuler = new Ruler(false);
		topRuler = new Ruler(true);
    }
    
    public void addNode(Element node) {
        nodes.add(node);
        fireStructureChange(PROP_NODE, nodes);
    }

    public void addNode(Element node,int index) {
    	if (index >= 0)
    		nodes.add(index, node);
		else
			nodes.add(node);
    	fireStructureChange(PROP_NODE, nodes);
    }

    public void removeNode(Element node) {
        nodes.remove(node);
        fireStructureChange(PROP_NODE, nodes);
    }

    public Ruler getRuler(int orientation) {
		Ruler result = null;
		switch (orientation) {
		case PositionConstants.NORTH:
			result = topRuler;
			break;
		case PositionConstants.WEST:
			result = leftRuler;
			break;
		}
		return result;
	}
    
    public List getNodes() {
        return nodes;
    }

    public InputStream getAsStream() throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(os);
        out.writeObject(this);
        out.close();
        InputStream istream = new ByteArrayInputStream(os.toByteArray());
        os.close();
        return istream;
    }

    public static Diagram makeFromStream(InputStream istream) throws IOException, ClassNotFoundException {
    	Diagram diagram;
    	if(istream.available() == 0){
    		diagram = new Diagram();
    	}else{
    		ObjectInputStream ois = new ObjectInputStream(istream);
    		diagram = (Diagram) ois.readObject();
    		ois.close();
    	}
    	return diagram;
    }

	@Override
	public Object getEditableValue() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IPropertyDescriptor[] getPropertyDescriptors() {
		// TODO Auto-generated method stub
		return new IPropertyDescriptor[]{};
	}

	@Override
	public Object getPropertyValue(Object arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isPropertySet(Object arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void resetPropertyValue(Object arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setPropertyValue(Object arg0, Object arg1) {
		// TODO Auto-generated method stub
		
	}

	public Ruler getLeftRuler() {
		return leftRuler;
	}

	public void setLeftRuler(Ruler leftRuler) {
		this.leftRuler = leftRuler;
	}

	public Ruler getTopRuler() {
		return topRuler;
	}

	public void setTopRuler(Ruler topRuler) {
		this.topRuler = topRuler;
	}

	public boolean isRulersVisibility() {
		return rulersVisibility;
	}

	public void setRulersVisibility(boolean rulersVisibility) {
		this.rulersVisibility = rulersVisibility;
	}

	
	public boolean isSnapToGeometry() {
		return snapToGeometry;
	}

	public void setSnapToGeometry(boolean snapToGeometry) {
		this.snapToGeometry = snapToGeometry;
	}

	public boolean isGridEnabled() {
		return gridEnabled;
	}

	public void setGridEnabled(boolean gridEnabled) {
		this.gridEnabled = gridEnabled;
	}

	public void setNodes(List nodes) {
		this.nodes = nodes;
	}

    
}